package com.wenc.psi.vmd;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.wenc.psi.bean.SaleBean;
import com.wenc.psi.bean.SaleDetailBean;
import com.wenc.psi.pojo.Part;
import com.wenc.psi.pojo.Sale;
import com.wenc.psi.pojo.SaleDetail;

import java.sql.Date;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

public class SaleViewModel extends AndroidViewModel {

    private MutableLiveData<SaleBean> saleLive;
    private MutableLiveData<LinkedList<SaleDetailBean>> saleDetailLive;

    public MutableLiveData<SaleBean> getSaleLive() {
        return saleLive;
    }

    public MutableLiveData<LinkedList<SaleDetailBean>> getSaleDetailLive() {
        return saleDetailLive;
    }

    public SaleViewModel(@NonNull Application application) {
        super(application);
        saleLive = new MutableLiveData<>();
        saleDetailLive = new MutableLiveData<>();
    }

    // 初始化数据
    public void initSale(){
        if (saleLive == null) saleLive = new MutableLiveData<>();
        if (saleDetailLive == null) saleDetailLive = new MutableLiveData<>();
        SaleBean saleBean = new SaleBean();
        String id = new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date());
        Date date = new Date(System.currentTimeMillis());
        Time time = new Time(System.currentTimeMillis());
        saleBean.setId(id);
        saleBean.setDate(date);
        saleBean.setTime(time);
        saleBean.setTotal(0.00);
        saleBean.setAddition(0.00);
        saleBean.setDiscount(1.00);
        saleLive.setValue(saleBean);
        LinkedList<SaleDetailBean> saleDetailBeans = new LinkedList<>();
        saleDetailLive.setValue(saleDetailBeans);
    }

    // 关闭资源
    public void close(){
        saleLive = null;
        saleDetailLive = null;
    }

    // 设置经手人
    public void setEmployee(String name, String id){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setEmployeeName(name);
        saleBean.setEmployeeID(id);
        saleLive.setValue(saleBean);
    }

    // 设置客户
    public void setCustomer(String name, String id){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setCustomerName(name);
        saleBean.setCustomerID(id);
        saleLive.setValue(saleBean);
    }

    // 添加配件
    public void addPart(Part... parts){
        LinkedList<SaleDetailBean> saleDetailBeans = saleDetailLive.getValue();
        for (int i = 0; i < parts.length; i++){
            SaleDetailBean saleDetailBean = new SaleDetailBean();
            saleDetailBean.setSaleId(saleLive.getValue().getId());
            saleDetailBean.setSerialNumber(saleDetailBeans.size() + 1);
            saleDetailBean.setPartId(parts[i].getId());
            saleDetailBean.setPartName(parts[i].getName());
            saleDetailBean.setPrice(parts[i].getPrice());
            saleDetailBean.setQuantity(1);
            saleDetailBeans.add(saleDetailBean);
        }
        saleDetailLive.setValue(saleDetailBeans);
        SaleBean saleBean = saleLive.getValue();
        saleBean.setTotal(total(saleBean));
        saleBean.setSaleDetailBeanLinkedList(saleDetailBeans);
        saleLive.setValue(saleBean);
    }

    // 设置折扣
    public void setDiscount(double discount){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setDiscount(discount);
        saleBean.setTotal(total(saleBean));
        saleLive.setValue(saleBean);
    }

    // 设置额外费用
    public void setAddition(double addition){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setAddition(addition);
        saleBean.setTotal(total(saleBean));
        saleLive.setValue(saleBean);
    }

    // 设置收款情况
    public void setPaymentStatus(String paymentStatus){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setPaymentStatus(paymentStatus);
        saleLive.setValue(saleBean);
    }

    // 设置收款方式
    public void setPaymentMethod(String paymentMethod){
        SaleBean saleBean = saleLive.getValue();
        saleBean.setPaymentMethod(paymentMethod);
        saleLive.setValue(saleBean);
    }

    // 设置总额
    private double total(SaleBean saleBean){
        LinkedList<SaleDetailBean> saleDetailBeans = saleDetailLive.getValue();
        double total = 0.00;
        for (SaleDetailBean saleDetailBean : saleDetailBeans) {
            total += saleDetailBean.getPrice() * saleDetailBean.getQuantity();
        }
        total *= saleBean.getDiscount();
        total += saleBean.getAddition();
        return total;
    }
}
